package com.qf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.mapper.EmployeeMapper;
import com.qf.pojo.Employee;
import com.qf.pojo.EmployeeVo;
import com.qf.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl  extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper mapper;


    @Override
    @Transactional
    public int addEmp(EmployeeVo employeeVo) {
        int result = 0;

        Employee emp = mapper.findEmpByName(employeeVo.getEmpName());
        if (emp == null){
            //result =mapper.insert(employeeVo);
            result =mapper.insertOneEmp(employeeVo);
        }else {
            result = -1;
        }
        return result;
    }

    @Override
    @Transactional
    public int dropEmp(Integer empId) {
       // return mapper.deleteById(empId);
        boolean b = removeById(empId);
        if (b){
            return 1;
        }else {
            return 0;
        }
        //return mapper.deleteOneEmp(empId);
    }

    @Override
    @Transactional
    public int editEmp(EmployeeVo employeeVo) {
        int result = 0;
        result =mapper.update(employeeVo,null);
        //result = mapper.updateOneEmp(employeeVo);
        return result;
    }

    @Override
    public List<EmployeeVo> getEmpsAndDept() {
        List<EmployeeVo> emps = mapper.findEmpsAndDept();
        return emps;
    }

    @Override
    public EmployeeVo getEmpAndDeptByEmpId(Integer empId) {
        return mapper.findEmpAndDeptById(empId);
    }

    @Override
    public PageInfo<EmployeeVo> getEmpsAndDeptByPage(Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<EmployeeVo> empsAndDept = mapper.findEmpsAndDept();
        PageInfo<EmployeeVo> pageInfo = new PageInfo<>(empsAndDept);
        return pageInfo;
    }



}
